﻿using HtmlParserLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace HtmlParserTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //var str = "http://tool.chinaz.com/regex/index[1-3].htm";

            //foreach (var item in GetUriList(str))
            //{
            //    Console.WriteLine(item.ToString());
            //}

            var s = " 1999年08月30日 00:00:00 ";
            DateTime d;
            var r = DateTime.TryParse(s.Trim(), out d);
            if (r)
            {
                Console.WriteLine(d.ToString("yyyy-MM-dd hh:mm:ss"));
            }
        }

        private static List<Uri> GetUriList(string urlConfig)
        {
            List<Uri> list = new List<Uri>();

            try
            {
                string[] patterns = new string[] { @"\[\w+-\w+\]", @"\(\w+-\w+\)" };
                string numPattern = @"\d+-\d+";
                foreach (var pattern in patterns)
                {
                    Regex r = new Regex(pattern);
                    var m = r.Match(urlConfig);
                    if (m.Success)
                    {

                        if (pattern == patterns[0])                     //如果是[]的则还取去掉括号的网址，例如：index[1-20].htm,则取index.htm,index1.htm,index2.htm.....
                        {
                            list.Add(new Uri(r.Replace(urlConfig, "")));
                        }
                        //找到前缀
                        var prefix = Regex.Replace(m.Value, numPattern, "");
                        prefix=prefix.Replace("(","");
                        prefix = prefix.Replace(")", "");
                        prefix = prefix.Replace("[", "");
                        prefix = prefix.Replace("]", "");

                        var d = Regex.Match(m.Value, numPattern);                        
                        string[] nums = d.Value.Split('-');
                        int f = int.Parse(nums[0]);
                        int to = int.Parse(nums[1]);

                        for (int i = f; i <= to; i++)
                        {
                            var target = r.Replace(urlConfig, prefix + i.ToString());
                            list.Add(new Uri(target));
                        }
                        break;
                    }
                }
            }
            catch
            {
                Uri item = new Uri(urlConfig);
                list.Add(item);
            }
            finally
            {
                if (list.Count == 0)
                {
                    Uri item = new Uri(urlConfig);
                    list.Add(item);
                }
            }

            return list;
        }
    }
}
